﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DebugAnts
{
    class ZoneManager
    {
        public int zoneSize;
        public int nbZoneRows, nbZoneCols;
        public List<List<Zone>> zones;

        public ZoneManager(int zoneSize_, int gridRows, int gridCols)
        {
            zoneSize = zoneSize_;
            nbZoneRows = (int)Math.Ceiling(((double)gridRows / zoneSize));
            nbZoneCols = (int)Math.Ceiling(((double)gridCols / zoneSize));
            int col;
            zones = new List<List<Zone>>();
            List<Zone> zoneRow;
            for(int row = 0; row < nbZoneRows; ++row)
            {
                zoneRow = new List<Zone>(nbZoneCols);
                for (col = 0; col < nbZoneCols; ++col)
                {
                    zoneRow.Add(new Zone());
                }
                zones.Add(zoneRow);
            }
        }

        public void SetTurnZone(Turn turn)
        {
            int col;
            int zoneRow, zoneCol;
            for (int row = 0; row < nbZoneRows; ++row)
            {
                for (col = 0; col < nbZoneCols; ++col)
                {
                    zones[row][col].enemyValue = zones[row][col].allyValue = 0.0f;
                }
            }
            for (int row = 0; row < Turn.sizeRows; ++row)
            {
                for (col = 0; col < Turn.sizeCols; ++col)
                {
                    if(turn.grid[row][col].ant > 0)
                    {
                        zoneRow = (int)Math.Ceiling(((row + 1.0) / zoneSize)) - 1;
                        zoneCol = (int)Math.Ceiling(((col + 1.0) / zoneSize)) - 1;
                        zones[zoneRow][zoneCol].enemyValue += 1.0f;
                    }
                    else if(turn.grid[row][col].ant == 0)
                    {
                        zoneRow = (int)Math.Ceiling(((row + 1.0) / zoneSize)) - 1;
                        zoneCol = (int)Math.Ceiling(((col + 1.0) / zoneSize)) - 1;
                        switch(turn.grid[row][col].antType)
                        {
                            case 1:
                                zones[zoneRow][zoneCol].allyValue += 0.1f;
                                break;
                            case 2:
                                zones[zoneRow][zoneCol].allyValue += 1.0f;
                                break;
                            case 3:
                                zones[zoneRow][zoneCol].allyValue += 2.0f;
                                break;
                        }
                    }
                }
            }
        }
    }
    class Zone
    {
        public float enemyValue, allyValue;

        public Zone()
        {
            enemyValue = allyValue = 0.0f;
        }
    }
}
